<?php
/**
 * CommonAction
 * 公用父类 对全局进行限制处理等
 */

class CommonAction extends Action {
    public $UserInfo = array();

    /**
     * 获得登录用户的头像地址
     * @return mixed|string
     */
    public function getLogoAdd($user_id) {
        $User = M('user_base_info');
        $UserInfo = $User->where("user_code = $user_id")->find();
        if (empty($UserInfo['user_logo_add'])) {
            if (empty($UserInfo['api_logo_add'])) {
                $user_logo_add = __PUBLIC__ . "/Uploads/Photo/default_160.png";
            } else {
                //如果是qq用户
                if ($UserInfo['user_reg_type'] == 3) {
                    $user_logo_add = str_replace('/40', '/100', $UserInfo['api_logo_add']);
                } else {
                    //新浪用户
                    $user_logo_add = str_replace('/50/', '/180/', $UserInfo['api_logo_add']);
                }
            }
        } else {
            $user_logo_add = __PUBLIC__ . "/Uploads/Photo/" . str_replace('_64', '_160', $UserInfo['user_logo_add']);
        }
        return $user_logo_add;
    }

    function _initialize() {
        global $UserInfo;
        $this->UserInfo = & $UserInfo;
        // 用户权限检查
        // 检测是否开启验证机制并且检测当前模块是否在不验证列表中
        if (C('USER_AUTH_ON') && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
            if (!session(C('USER_AUTH_KEY'))) {
                //未登录时，判断是否有cookie，2周内免登录
                if (cookie('user_email') && cookie('user_passwd')) {
                    //自动登陆
                    $user_email = cookie('user_email');
                    $user_passwd = cookie('user_passwd');
                    $User = M('user_base_info');
                    $rows = $User->where("user_email = '$user_email' and user_passwd = '$user_passwd'")->find();
                    //获取用户信息
                    session(C('USER_AUTH_KEY'), $rows['user_code']);
                    session('user_email', $rows['user_email']);
                    session('user_nickname', $rows['user_nickname']);
                    session('user_province', $rows['user_province']);
                    session('user_city', $rows['user_city']);
                    session('user_industry', $rows['user_industry']);
                    session('api_logo_add', $rows['api_logo_add']);
                    session('user_logo_add', $rows['user_logo_add']);
                    session('user_gender', $rows['user_gender']);
                    session('user_birth_date', $rows['user_birth_date']);
                    session('user_integral', $rows['user_integral']);
                } else {
                    redirect(PHP_FILE . '/Public/login'); //跳转到登录页面
                }
            } else {
                //已登录
            }
        }
        $UserInfo['user_code'] = session(C('USER_AUTH_KEY'));
        $UserInfo['user_email'] = session('user_email');
        $UserInfo['user_nickname'] = session('user_nickname');
        $UserInfo['user_logo_add'] = $this->getLogoAdd(session(C('USER_AUTH_KEY')));
        $UserInfo['user_province'] = session('user_province');
        $UserInfo['user_city'] = session('user_city');
        $UserInfo['user_industry'] = session('user_industry');
        $UserInfo['user_gender'] = session('user_gender');
        $UserInfo['user_birth_date'] = session('user_birth_date');
        $UserInfo['user_integral'] = session('user_integral');
        $this->assign('UserInfo', $UserInfo);
        //查找登陆用户的好友数
        /*		$F = M('user_friend_info');
                $id_str = $F->where("user_code = $user_code")->getField('id_str');
                if ($id_str){
                    $arr = explode(',',$id_str);
                    $count = count($arr);
                }else{
                    $count = 0;
                }
                $this->assign('firend_count',$count);*/
    }


    /**
     * 发送邮件
     * @param $email_type int 邮件类型，1为注册激活邮件，2为系统下发的推荐邮件，3为找回密码邮件, 4为绑定账号邮件
     * @param $email_address array() 邮箱地址
     * @param $email_subject string 邮件标题
     * @param $email_content string 邮件内容
     * @param $email_key string 邮件唯一标识串
     * @param $insert_data int 是否需要写入数据库 默认为0不需要，1为需要
     */
    function sendEmail($email_type = 1, $email_address = array(), $email_subject, $email_content, $email_key = '', $insert_data = 0) {
        require_once(LIB_PATH . 'ORG/Phpmailer.class.php');
        $mail = new PHPMailer;
        $mail->IsSMTP(); // set mailer to use SMTP
        $mail->SMTPDebug = 1;
        $mail->SMTPAuth = true;
        $mail->Host = "smtp.sina.com"; // sets the SMTP server
        $mail->Port = 25; // set the SMTP port for the GMAIL server
        $mail->Username = "mcskong@sina.com"; // SMTP account username
        $mail->Password = "1qaz@WSX"; // SMTP account password
        $mail->From = "mcskong@sina.com";
        $mail->FromName = "24淘";
        $mail->CharSet = "utf-8";
        $mail->Encoding = "base64";
        //$mail->AddReplyTo("info@site.com", "Information");
        //$mail->WordWrap = 50;    // 自动换行的字数
        //$mail->AddAttachment("c:/temp/11-10-00.zip");
        $mail->IsHTML(true); // set email format to HTML
        $mail->AddAddress(implode(',', $email_address));
        $mail->Subject = $email_subject;
        $mail->Body = $email_content;
        if ($mail->Send()) {
            if ($insert_data == 1) {
                $Email = M('user_sendemail_list');
                foreach ($email_address as $email) {
                    $data['user_email'] = $email;
                    $data['email_type'] = $email_type;
                    if ($email_key) $data['email_key'] = $email_key;
                    $Email->add($data);
                    unset($data);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 传递用户id，返回需要的用户字段信息，默认返回用户昵称
     * @param $user_id
     * @param string $field
     */
    function getUserField($user_id, $field = 'user_nickname') {
        $User = M('user_base_info');
        $field = $User->where("user_code = $user_id")->getField($field);
        return $field;
    }

	//明星操作AJAX调用
    public function addFollow() {
        $type = $_GET['type'];
        $id = $_GET['id'];
        //验证是否登录
		$user_id = session(C('USER_AUTH_KEY'));
		if ( !$user_id ) {
			$this->ajaxReturn(false);
		}else{
			$Star = M('star');
			$starInfo = $Star->where("id=$id")->find();
			if ($type == 'like') {
				$data['like_num'] = $starInfo['like_num'] + 1;
				$Star->where("id=$id")->save($data);

				$Relation = M('user_relation_info');
				$realtionData['user_code'] = $user_id;
				$realtionData['user_relation_code'] = $id;
				$realtionData['user_relation_type'] = 2;
				$realtionData['create_time'] = date('Y-m-d');
				$Relation->add($realtionData);
				$this->ajaxReturn($data['like_num']);
			}
			
		}

        
    }

}

?>